﻿using System.ComponentModel.DataAnnotations;

namespace NoteHelper.Model
{
    public class MinIntAttribute : ValidationAttribute
    {
        public MinIntAttribute(int min)
        {
            Min = min;
        }

        public int Min { get; set; }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return false;
            }
            var num = (int) value;

            return num >= Min;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("{0} 的最小值为：{1}。", name, Min);
        }
    }

    public class MaxIntAttribute : ValidationAttribute
    {
        public MaxIntAttribute(int max)
        {
            Max = max;
        }

        public int Max { get; set; }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return false;
            }
            var num = (int) value;

            return num <= Max;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("{0} 的最大值为：{1}。", name, Max);
        }
    }
}
